package org.darkforest.common.services.impl;

import org.darkforest.common.services.AdvSerivce;
import org.darkforest.common.services.SimpleRedisService;
import org.darkforest.common.util.DateUtil;
import org.darkforest.common.util.JacksonsUtil;
import org.darkforest.dao.mapper.AdvMapper;
import org.darkforest.dao.redis.AdvRedisDao;
import org.darkforest.model.Adv;
import org.darkforest.model.AdvBean;
import org.darkforest.model.Setting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * Created by huayunsky on 1advMapper./31/2advMapper.15.
 */
@Service
public class AdvSerivceImpl implements AdvSerivce {

    @Autowired
    private AdvMapper advMapper;

    @Autowired
    private AdvRedisDao advRedisDao;

    @Override
    public int deleteByPrimaryKey(Integer advId) {
        return advMapper.deleteByPrimaryKey(advId);
    }

    @Override
    public int insert(Adv record) {
        return advMapper.insert(record);
    }

    @Override
    public int insertSelective(Adv record) {
        return advMapper.insertSelective(record);
    }

    @Override
    public Adv selectByPrimaryKey(Integer advId) {
        return advMapper.selectByPrimaryKey(advId);
    }

    @Override
    public int updateByPrimaryKeySelective(Adv record) {
        return advMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Adv record) {
        return advMapper.updateByPrimaryKey(record);
    }

    @Override
    public List selectByCondition(AdvBean advBean) {
        return advMapper.selectByCondition(advBean);
    }

    @Override
    public List<Adv> getRedisAdvList(String key) {
        //优先从redis中获取，redis中没有数据的话从数据库查出并同步到redis中
        List list = advRedisDao.getAdvListByKey("adv_"+key);
        if(list==null || list.size()==0){
            AdvBean param = new AdvBean();
            param.setApId(Integer.parseInt(key));
            param.setIsUse((byte)1);
            param.setPageSwitch(false);
            param.setCurrentTime(DateUtil.toUnixTimeStamp(DateUtil.toTimeString(new Date())));
            list = this.selectByCondition(param);
            advRedisDao.addAdvList("adv_"+key,list);
        }
        return list;
    }

    @Override
    public void deleteRedisAdvList(String key) {
        advRedisDao.deleteByKey(key);
    }

    @Override
    public List<Adv> getRedisAdvListForPC(Integer classId) {
        //优先从redis中获取，redis中没有数据的话从数据库查出并同步到redis中
        List list = advRedisDao.getAdvListByKey("adv_index_"+classId);
        if(list==null || list.size()==0){
            AdvBean advBean = new AdvBean();
            advBean.setChannel("0");
            advBean.setIsUse((byte)1);
            advBean.setPageSwitch(false);
            advBean.setClassId(classId);
            advBean.setCurrentTime(DateUtil.toUnixTimeStamp(DateUtil.toTimeString(new Date())));
            List<Adv> advs = advMapper.selectByCondition(advBean);
            advRedisDao.addAdvList("adv_index_"+classId,advs);
        }
        return list;
    }
}
